package com.example.asa;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DisasterDAO {

    public static List<Disaster> getAllDisasters() throws SQLException {
        List<Disaster> disasterList = new ArrayList<>();
        String query = "SELECT * FROM disasters";

        try (Connection conn = DatabaseUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(query)) {

            while (rs.next()) {
                Disaster disaster = new Disaster();
                disaster.setId(rs.getInt("id"));
                disaster.setName(rs.getString("name")); // 映射 event_name 字段
                disaster.setTimestamp(rs.getTimestamp("timestamp")); // 映射 event_time 字段
                disaster.setLocation(rs.getString("location"));
                disaster.setAffectedArea(rs.getString("affected_area"));
                disaster.setDescription(rs.getString("description"));
                disaster.setStatus(rs.getString("status"));
                disaster.setSeverity(rs.getString("severity"));
                disaster.setContact(rs.getString("contact"));
                disaster.setInitialSolution(rs.getString("initial_solution")); // 映射 initial_solution 字段

                disasterList.add(disaster);
            }
        }

        return disasterList;
    }



    public static boolean addDisaster(Disaster disaster) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = MySQLDatabaseConnector.getConnection();
            if (conn == null) return false;

            String sql = "INSERT INTO disasters (name, timestamp, location, affected_area, description, status, severity, contact) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, disaster.getName());
            stmt.setTimestamp(2, disaster.getTimestamp());
            stmt.setString(3, disaster.getLocation());
            stmt.setString(4, disaster.getAffectedArea());
            stmt.setString(5, disaster.getDescription());
            stmt.setString(6, disaster.getStatus());
            stmt.setString(7, disaster.getSeverity());
            stmt.setString(8, disaster.getContact());

            return stmt.executeUpdate() > 0;

        } catch (SQLException e) {
            System.err.println("Error adding disaster: " + e.getMessage());
            return false;
        } finally {
            try { if (stmt != null) stmt.close(); } catch (SQLException e) {}
            try { if (conn != null) conn.close(); } catch (SQLException e) {}
        }
    }

    public static boolean updateDisaster(Disaster disaster) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = MySQLDatabaseConnector.getConnection();
            if (conn == null) return false;

            String sql = "UPDATE disasters SET name=?, timestamp=?, location=?, affected_area=?, description=?, status=?, severity=?, contact=? WHERE id=?";
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, disaster.getName());
            stmt.setTimestamp(2, disaster.getTimestamp());
            stmt.setString(3, disaster.getLocation());
            stmt.setString(4, disaster.getAffectedArea());
            stmt.setString(5, disaster.getDescription());
            stmt.setString(6, disaster.getStatus());
            stmt.setString(7, disaster.getSeverity());
            stmt.setString(8, disaster.getContact());
            stmt.setInt(9, disaster.getId());

            return stmt.executeUpdate() > 0;

        } catch (SQLException e) {
            System.err.println("Error updating disaster: " + e.getMessage());
            return false;
        } finally {
            try { if (stmt != null) stmt.close(); } catch (SQLException e) {}
            try { if (conn != null) conn.close(); } catch (SQLException e) {}
        }
    }

    public static boolean deleteDisaster(int id) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = MySQLDatabaseConnector.getConnection();
            if (conn == null) return false;

            String sql = "DELETE FROM disasters WHERE id=?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);

            return stmt.executeUpdate() > 0;

        } catch (SQLException e) {
            System.err.println("Error deleting disaster: " + e.getMessage());
            return false;
        } finally {
            try { if (stmt != null) stmt.close(); } catch (SQLException e) {}
            try { if (conn != null) conn.close(); } catch (SQLException e) {}
        }
    }
}
